package com.htc.android.footprints.utils.camera;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import com.htc.android.footprints.utils.FootprintUtils;
import dalvik.system.VMRuntime;
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class FootprintImageManager
{
  private static final boolean DEBUG = false;
  private static final String TAG = "FootprintImageManager";
  private static FootprintImageManager sInstance;
  private static volatile Matrix sScaleMatrix;
  private static Uri sStorageURI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  public Map<String, FastBitmapDrawable> cachedMap = Collections.synchronizedMap(new HashMap());
  private String[] projection;

  static
  {
    sInstance = null;
  }

  public FootprintImageManager()
  {
    String[] arrayOfString = new String[4];
    arrayOfString[0] = "_id";
    arrayOfString[1] = "_display_name";
    arrayOfString[2] = "_data";
    arrayOfString[3] = "date_added";
    this.projection = arrayOfString;
  }

  private static boolean checkFsWritable()
  {
    String str = Environment.getExternalStorageDirectory().toString() + "/.footprints/thumbnails";
    File localFile1 = new File(str);
    boolean i;
    if ((!localFile1.isDirectory()) && (!localFile1.mkdirs()))
      i = 0;
    while (true)
    {
      return i;
      File localFile2 = new File(str, ".probe");
      try
      {
        if (localFile2.exists())
          localFile2.delete();
        if (!localFile2.createNewFile())
        {
          i = 0;
          continue;
        }
        localFile2.delete();
        i = 1;
      }
      catch (IOException localIOException)
      {
        i = 0;
      }
    }
  }

  private static int computeSampleSize(BitmapFactory.Options paramOptions, int paramInt)
  {
    int i = paramOptions.outWidth;
    int j = paramOptions.outHeight;
    int k = Math.max(i / paramInt, j / paramInt);
    if (k == 0);
    for (int m = 1; ; m = k)
    {
      return m;
      if ((k > 1) && (i > paramInt) && (i / k < paramInt))
        k--;
      if ((k <= 1) || (j <= paramInt) || (j / k >= paramInt))
        continue;
      k--;
    }
  }

  public static Bitmap copyBitmap(Bitmap paramBitmap1, Bitmap paramBitmap2, int paramInt1, int paramInt2, int paramInt3, int paramInt4, Matrix paramMatrix, boolean paramBoolean)
  {
    Canvas localCanvas = new Canvas();
    Rect localRect = new Rect(paramInt1, paramInt2, paramInt1 + paramInt3, paramInt2 + paramInt4);
    RectF localRectF1 = new RectF(0.0F, 0.0F, paramInt3, paramInt4);
    RectF localRectF2 = new RectF();
    paramMatrix.mapRect(localRectF2, localRectF1);
    int i = Math.round(localRectF2.width());
    int j = Math.round(localRectF2.height());
    localCanvas.translate(-localRectF2.left, -localRectF2.top);
    localCanvas.concat(paramMatrix);
    Paint localPaint = new Paint();
    localPaint.setFilterBitmap(paramBoolean);
    if (!paramMatrix.rectStaysRect())
      localPaint.setAntiAlias(true);
    if (paramBitmap1 == null)
      paramBitmap1 = Bitmap.createBitmap(i, j, Bitmap.Config.RGB_565);
    localCanvas.setBitmap(paramBitmap1);
    localCanvas.drawBitmap(paramBitmap2, localRect, localRectF1, localPaint);
    return paramBitmap1;
  }

  // ERROR //
  public static Bitmap createScaledBitmap(Bitmap paramBitmap1, Bitmap paramBitmap2, int paramInt1, int paramInt2, boolean paramBoolean)
  {
  }

  public static String getDirectory()
  {
    return getExternalStorageDirectory() + "/.footprints/thumbnails";
  }

  public static String getExternalStorageDirectory()
  {
    return Environment.getExternalStorageDirectory().getAbsolutePath();
  }

  public static String getFootprintsDirectory()
  {
    return getExternalStorageDirectory() + File.separator + ".footprints";
  }

  public static FootprintImageManager getInstance()
  {
    if (sInstance == null)
      sInstance = new FootprintImageManager();
    return sInstance;
  }

  public static boolean hasStorage()
  {
    return hasStorage(true);
  }

  public static boolean hasStorage(boolean paramBoolean)
  {
    String str = Environment.getExternalStorageState();
    boolean bool;
    if ("mounted".equals(str))
      if (paramBoolean)
        bool = checkFsWritable();
    while (true)
    {
      return bool;
      bool = true;
      continue;
      if ((!paramBoolean) && ("mounted_ro".equals(str)))
      {
        bool = true;
        continue;
      }
      bool = false;
    }
  }

  public static Cursor query(Context paramContext, Uri paramUri, String[] paramArrayOfString1, String paramString1, String[] paramArrayOfString2, String paramString2)
  {
    Object localObject;
    try
    {
      ContentResolver localContentResolver = paramContext.getContentResolver();
      if (localContentResolver == null)
      {
        localObject = null;
      }
      else
      {
        Cursor localCursor = localContentResolver.query(paramUri, paramArrayOfString1, paramString1, paramArrayOfString2, paramString2);
        localObject = localCursor;
      }
    }
    catch (UnsupportedOperationException localUnsupportedOperationException)
    {
      localObject = null;
    }
    return localObject;
  }

  public static int roundOrientation(int paramInt)
  {
    int i = paramInt;
    if (i == -1)
      i = 0;
    int j = i % 360;
    int k;
    if (j < 45)
      k = 0;
    while (true)
    {
      return k;
      if (j < 135)
      {
        k = 90;
        continue;
      }
      if (j < 225)
      {
        k = 180;
        continue;
      }
      if (j < 315)
      {
        k = 270;
        continue;
      }
      k = 0;
    }
  }

  public Uri addImage(ContentResolver paramContentResolver, String paramString1, String paramString2, long paramLong, Location paramLocation, int paramInt, String paramString3, String paramString4)
  {
    ContentValues localContentValues = new ContentValues(7);
    localContentValues.put("title", paramString1);
    localContentValues.put("_display_name", paramString1 + ".jpg");
    localContentValues.put("description", paramString2);
    localContentValues.put("datetaken", Long.valueOf(paramLong));
    localContentValues.put("mime_type", "image/jpeg");
    localContentValues.put("orientation", Integer.valueOf(paramInt));
    File localFile = new File(paramString3);
    if (!localFile.exists())
      localFile.mkdirs();
    String str1 = localFile.toString().toLowerCase();
    String str2 = localFile.getName().toLowerCase();
    localContentValues.put("bucket_id", Integer.valueOf(str1.hashCode()));
    localContentValues.put("bucket_display_name", str2);
    if (paramLocation != null)
    {
      localContentValues.put("latitude", Double.valueOf(paramLocation.getLatitude()));
      localContentValues.put("longitude", Double.valueOf(paramLocation.getLongitude()));
    }
    if ((paramString3 != null) && (paramString4 != null))
      localContentValues.put("_data", paramString3 + File.separator + paramString4);
    return paramContentResolver.insert(sStorageURI, localContentValues);
  }

  /** @deprecated */
  public boolean cachedScaledFastImage(String paramString1, int paramInt1, int paramInt2, String paramString2)
  {
    monitorenter;
    int i = 0;
    try
    {
      String str = paramString1 + "_" + paramInt1 + "_" + paramInt2 + "_" + paramString2;
      if (this.cachedMap.get(str) == null)
      {
        boolean bool = new File(paramString2).exists();
        if (!bool);
      }
      try
      {
        BitmapFactory.Options localOptions = new BitmapFactory.Options();
        localOptions.inDither = false;
        localOptions.inScaled = false;
        localOptions.inSampleSize = 8;
        Bitmap localBitmap1 = BitmapFactory.decodeFile(paramString2, localOptions);
        if (localBitmap1 != null)
        {
          Bitmap localBitmap2 = Bitmap.createScaledBitmap(localBitmap1, paramInt1, paramInt2, false);
          localBitmap1.recycle();
          FastBitmapDrawable localFastBitmapDrawable = new FastBitmapDrawable(localBitmap2);
          this.cachedMap.put(str, localFastBitmapDrawable);
        }
        while (true)
        {
          i = 1;
          label170: monitorexit;
          return i;
        }
      }
      catch (Exception localException)
      {
        break label170;
      }
    }
    finally
    {
      monitorexit;
    }
    throw localObject;
  }

  /** @deprecated */
  public void clearCache(String paramString)
  {
    monitorenter;
    try
    {
      Log.d("FootprintImageManager", "Howard - before clear cache:" + VMRuntime.getRuntime().getExternalBytesAllocated());
      Iterator localIterator = this.cachedMap.keySet().iterator();
      while (localIterator.hasNext())
      {
        String str = (String)localIterator.next();
        if (!str.startsWith(paramString + "_"))
          continue;
        FastBitmapDrawable localFastBitmapDrawable = (FastBitmapDrawable)this.cachedMap.get(str);
        if (localFastBitmapDrawable == null)
          continue;
        this.cachedMap.put(str, null);
        localFastBitmapDrawable.recycle();
      }
    }
    finally
    {
      monitorexit;
    }
    Log.d("FootprintImageManager", "Howard - aftr clear cache:" + VMRuntime.getRuntime().getExternalBytesAllocated());
    monitorexit;
  }

  /** @deprecated */
  public void clearCachebyKeyword(String paramString)
  {
    monitorenter;
    try
    {
      Iterator localIterator = this.cachedMap.keySet().iterator();
      while (localIterator.hasNext())
      {
        String str = (String)localIterator.next();
        if (str.indexOf(paramString) == -1)
          continue;
        FastBitmapDrawable localFastBitmapDrawable = (FastBitmapDrawable)this.cachedMap.get(str);
        if (localFastBitmapDrawable == null)
          break;
        this.cachedMap.put(str, null);
        localFastBitmapDrawable.recycle();
      }
      monitorexit;
      return;
    }
    finally
    {
      localObject = finally;
      monitorexit;
    }
    throw localObject;
  }

  public boolean forceWriteToFile(ContentResolver paramContentResolver, Bitmap paramBitmap, String paramString)
  {
    if ((paramBitmap == null) || (paramContentResolver == null) || (paramString == null));
    for (int i = 0; ; i = 1)
    {
      return i;
      writeToFile(paramBitmap, getDirectory() + File.separator + paramString);
      FootprintUtils.forceSync(paramContentResolver, paramString);
    }
  }

  public boolean forceWriteToFile(ContentResolver paramContentResolver, byte[] paramArrayOfByte, String paramString)
  {
    if ((paramArrayOfByte.length == 0) || (paramContentResolver == null) || (paramString == null));
    for (int i = 0; ; i = 1)
    {
      return i;
      writetoFile(paramArrayOfByte, paramString);
      FootprintUtils.forceSync(paramContentResolver, paramString);
    }
  }

  public Bitmap getImage(Uri paramUri, Context paramContext)
  {
    Object localObject;
    if (paramUri != null)
      try
      {
        Cursor localCursor = paramContext.getContentResolver().query(paramUri, this.projection, null, null, null);
        if (localCursor.getCount() > 0)
        {
          localCursor.moveToFirst();
          localObject = BitmapFactory.decodeFile(localCursor.getString(2));
          break label108;
        }
        Bitmap localBitmap = BitmapFactory.decodeResource(paramContext.getResources(), 2130837758);
        localObject = localBitmap;
      }
      catch (Exception localException)
      {
        Log.d("FootprintImageManager", "getimage error");
        localObject = BitmapFactory.decodeResource(paramContext.getResources(), 2130837758);
      }
    else
      localObject = BitmapFactory.decodeResource(paramContext.getResources(), 2130837758);
    label108: return (Bitmap)localObject;
  }

  public Bitmap getImage(File paramFile, Context paramContext)
  {
    Object localObject = null;
    if ((paramFile != null) && (paramFile.exists()));
    try
    {
      localObject = BitmapFactory.decodeFile(paramFile.getAbsolutePath());
      if (localObject == null)
      {
        Bitmap localBitmap = BitmapFactory.decodeResource(paramContext.getResources(), 2130837758);
        localObject = localBitmap;
      }
      if (localObject == null)                                                                                   
        localObject = BitmapFactory.decodeResource(paramContext.getResources(), 2130837758);
      return localObject;
    }
    catch (Exception localException)
    {
      while (true)
        Log.w("FootprintImageManager", localException.getMessage(), localException);
    }
  }

  public String getImagePath(Uri paramUri, Context paramContext)
  {
    if (paramUri != null);
    while (true)
    {
      try
      {
        Cursor localCursor = paramContext.getContentResolver().query(paramUri, this.projection, null, null, null);
        if (localCursor.getCount() <= 0)
          continue;
        localCursor.moveToFirst();
        String str2 = localCursor.getString(2);
        str1 = str2;
        return str1;
        str1 = null;
        continue;
      }
      catch (Exception localException)
      {
        str1 = null;
        continue;
      }
      String str1 = null;
    }
  }

  /** @deprecated */
  public Drawable getScaledFastImage(String paramString1, String paramString2, int paramInt1, int paramInt2)
  {
    monitorenter;
    if (paramString2 != null);
    try
    {
      boolean bool1 = paramString2.equals("");
      Object localObject1;
      if (bool1)
      {
        localObject1 = null;
        return localObject1;
      }
      String str = paramString1 + "_" + paramInt1 + "_" + paramInt2 + "_" + paramString2;
      FastBitmapDrawable localFastBitmapDrawable = (FastBitmapDrawable)this.cachedMap.get(str);
      if (localFastBitmapDrawable == null)
      {
        boolean bool2 = new File(paramString2).exists();
        if (!bool2)
          break label215;
      }
      while (true)
      {
        try
        {
          BitmapFactory.Options localOptions = new BitmapFactory.Options();
          localOptions.inDither = false;
          localOptions.inScaled = false;
          localOptions.inSampleSize = 8;
          Bitmap localBitmap3 = BitmapFactory.decodeFile(paramString2, localOptions);
          localBitmap1 = localBitmap3;
          if (localBitmap1 == null)
            break label221;
          Bitmap localBitmap2 = Bitmap.createScaledBitmap(localBitmap1, paramInt1, paramInt2, false);
          localBitmap1.recycle();
          localFastBitmapDrawable = new FastBitmapDrawable(localBitmap2);
          this.cachedMap.put(str, localFastBitmapDrawable);
          localObject1 = localFastBitmapDrawable;
        }
        catch (Exception localException)
        {
          localBitmap1 = null;
          continue;
        }
        label215: Bitmap localBitmap1 = null;
        continue;
        label221: localFastBitmapDrawable = null;
      }
    }
    finally
    {
      monitorexit;
    }
    throw localObject2;
  }

  public Bitmap getScaledImage(Bitmap paramBitmap, File paramFile, Context paramContext, int paramInt1, int paramInt2, int paramInt3, boolean paramBoolean)
  {
    Object localObject = null;
    BitmapFactory.Options localOptions = new BitmapFactory.Options();
    localOptions.inDither = false;
    localOptions.inScaled = false;
    localOptions.inSampleSize = paramInt3;
    if ((paramFile != null) && (paramFile.exists()));
    try
    {
      localObject = BitmapFactory.decodeFile(paramFile.getAbsolutePath(), localOptions);
      if (localObject == null)
      {
        Bitmap localBitmap2 = BitmapFactory.decodeResource(paramContext.getResources(), 2130837758);
        localObject = localBitmap2;
      }
      if (localObject == null)
        localObject = BitmapFactory.decodeResource(paramContext.getResources(), 2130837758);
      Bitmap localBitmap1 = createScaledBitmap(paramBitmap, (Bitmap)localObject, paramInt1, paramInt2, paramBoolean);
      if (localObject != null)
        ((Bitmap)localObject).recycle();
      return localBitmap1;
    }
    catch (Exception localException)
    {
      while (true)
      {
        Log.w("FootprintImageManager", localException.getMessage(), localException);
        localObject = null;
      }
    }
  }

  public Bitmap getScaledImage(File paramFile, Context paramContext, int paramInt1, int paramInt2, boolean paramBoolean)
  {
    Object localObject = null;
    BitmapFactory.Options localOptions = new BitmapFactory.Options();
    localOptions.inDither = false;
    localOptions.inScaled = false;
    localOptions.inSampleSize = 2;
    if ((paramFile != null) && (paramFile.exists()));
    try
    {
      localObject = BitmapFactory.decodeFile(paramFile.getAbsolutePath(), localOptions);
      if (localObject == null)
      {
        Bitmap localBitmap2 = BitmapFactory.decodeResource(paramContext.getResources(), 2130837758);
        localObject = localBitmap2;
      }
      if (localObject == null)
        localObject = BitmapFactory.decodeResource(paramContext.getResources(), 2130837758);
      Bitmap localBitmap1 = Bitmap.createScaledBitmap((Bitmap)localObject, paramInt1, paramInt2, paramBoolean);
      if (localObject != null)
        ((Bitmap)localObject).recycle();
      return localBitmap1;
    }
    catch (Exception localException)
    {
      while (true)
      {
        Log.w("FootprintImageManager", localException.getMessage(), localException);
        localObject = null;
      }
    }
  }

  public boolean isCached(String paramString)
  {
    if (this.cachedMap.get(paramString) != null);
    for (int i = 1; ; i = 0)
      return i;
  }

  public boolean isCached(String paramString1, String paramString2, int paramInt1, int paramInt2)
  {
    String str = paramString1 + "_" + paramInt1 + "_" + paramInt2 + "_" + paramString2;
    if (this.cachedMap.get(str) != null);
    for (int i = 1; ; i = 0)
      return i;
  }

  // ERROR //
  public boolean writeToFile(Bitmap paramBitmap, String paramString)
  {
    // Byte code:
    //   0: aload_1
    //   1: ifnonnull +9 -> 10
    //   4: iconst_0
    //   5: istore 7
    //   7: iload 7
    //   9: ireturn
    //   10: new 521	java/io/FileOutputStream
    //   13: dup
    //   14: new 78	java/io/File
    //   17: dup
    //   18: aload_2
    //   19: invokespecial 92	java/io/File:<init>	(Ljava/lang/String;)V
    //   22: invokespecial 524	java/io/FileOutputStream:<init>	(Ljava/io/File;)V
    //   25: astore_3
    //   26: aload_1
    //   27: getstatic 530	android/graphics/Bitmap$CompressFormat:JPEG	Landroid/graphics/Bitmap$CompressFormat;
    //   30: bipush 85
    //   32: aload_3
    //   33: invokevirtual 534	android/graphics/Bitmap:compress	(Landroid/graphics/Bitmap$CompressFormat;ILjava/io/OutputStream;)Z
    //   36: pop
    //   37: aload_3
    //   38: invokevirtual 537	java/io/FileOutputStream:flush	()V
    //   41: aload_3
    //   42: invokevirtual 541	java/io/FileOutputStream:getFD	()Ljava/io/FileDescriptor;
    //   45: invokevirtual 546	java/io/FileDescriptor:sync	()V
    //   48: aload_3
    //   49: invokevirtual 549	java/io/FileOutputStream:close	()V
    //   52: iconst_1
    //   53: istore 7
    //   55: goto -48 -> 7
    //   58: astore 9
    //   60: aload 9
    //   62: astore 5
    //   64: ldc 15
    //   66: ldc_w 551
    //   69: invokestatic 554	android/util/Log:e	(Ljava/lang/String;Ljava/lang/String;)I
    //   72: pop
    //   73: aload 5
    //   75: invokevirtual 557	java/io/IOException:printStackTrace	()V
    //   78: goto -26 -> 52
    //   81: astore 4
    //   83: aload 4
    //   85: astore 5
    //   87: goto -23 -> 64
    //
    // Exception table:
    //   from	to	target	type
    //   10	26	58	java/io/IOException
    //   26	52	81	java/io/IOException
  }

  // ERROR //
  public boolean writetoFile(byte[] paramArrayOfByte, String paramString)
  {
    // Byte code:
    //   0: aload_1
    //   1: ifnull +71 -> 72
    //   4: new 78	java/io/File
    //   7: dup
    //   8: new 69	java/lang/StringBuilder
    //   11: dup
    //   12: invokespecial 70	java/lang/StringBuilder:<init>	()V
    //   15: invokestatic 455	com/htc/android/footprints/utils/camera/FootprintImageManager:getDirectory	()Ljava/lang/String;
    //   18: invokevirtual 86	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   21: getstatic 238	java/io/File:separator	Ljava/lang/String;
    //   24: invokevirtual 86	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   27: aload_2
    //   28: invokevirtual 86	java/lang/StringBuilder:append	(Ljava/lang/String;)Ljava/lang/StringBuilder;
    //   31: invokevirtual 89	java/lang/StringBuilder:toString	()Ljava/lang/String;
    //   34: invokespecial 92	java/io/File:<init>	(Ljava/lang/String;)V
    //   37: astore_3
    //   38: new 521	java/io/FileOutputStream
    //   41: dup
    //   42: aload_3
    //   43: invokespecial 524	java/io/FileOutputStream:<init>	(Ljava/io/File;)V
    //   46: astore 4
    //   48: aload 4
    //   50: aload_1
    //   51: invokevirtual 561	java/io/FileOutputStream:write	([B)V
    //   54: aload 4
    //   56: invokevirtual 537	java/io/FileOutputStream:flush	()V
    //   59: aload 4
    //   61: invokevirtual 541	java/io/FileOutputStream:getFD	()Ljava/io/FileDescriptor;
    //   64: invokevirtual 546	java/io/FileDescriptor:sync	()V
    //   67: aload 4
    //   69: invokevirtual 549	java/io/FileOutputStream:close	()V
    //   72: iconst_1
    //   73: ireturn
    //   74: astore 8
    //   76: aload 8
    //   78: astore 6
    //   80: ldc 15
    //   82: aload 6
    //   84: invokevirtual 562	java/lang/Exception:toString	()Ljava/lang/String;
    //   87: invokestatic 554	android/util/Log:e	(Ljava/lang/String;Ljava/lang/String;)I
    //   90: pop
    //   91: goto -19 -> 72
    //   94: astore 5
    //   96: aload 5
    //   98: astore 6
    //   100: goto -20 -> 80
    //
    // Exception table:
    //   from	to	target	type
    //   4	38	74	java/lang/Exception
    //   38	72	94	java/lang/Exception
  }

  public static class FastBitmapDrawable extends Drawable
  {
    private Bitmap mBitmap;

    public FastBitmapDrawable(Bitmap paramBitmap)
    {
      this.mBitmap = paramBitmap;
    }

    public void draw(Canvas paramCanvas)
    {
      try
      {
        paramCanvas.drawBitmap(this.mBitmap, 0.0F, 0.0F, null);
        return;
      }
      catch (Exception localException)
      {
        while (true)
          localException.printStackTrace();
      }
    }

    public Bitmap getBitmap()
    {
      return this.mBitmap;
    }

    public int getHeight()
    {
      return this.mBitmap.getHeight();
    }

    public int getOpacity()
    {
      return -1;
    }

    public int getWidth()
    {
      return this.mBitmap.getWidth();
    }

    public void recycle()
    {
      this.mBitmap.recycle();
    }

    public void setAlpha(int paramInt)
    {
    }

    public void setColorFilter(ColorFilter paramColorFilter)
    {
    }
  }
}

/* Location:           E:\workspace\dex2java\dex-translator-0.0.9.3\HtcFootprints_dex2jar.jar
 * Qualified Name:     com.htc.android.footprints.utils.camera.FootprintImageManager
 * JD-Core Version:    0.6.0
 */